﻿using System;
using System.Collections.Generic;
using Microsoft.Synchronization.SimpleProviders;

namespace Microsoft.WindowsAzure.Accelerator.Synchronization
{
    /// <summary>
    /// Basic unit of determination of file and blob changes.
    /// </summary>
    internal static class CloudSyncItemFields
    {
        public const UInt32 Name = 1;
        public const UInt32 Timestamp = 2;
        public const UInt32 ETag = 3;

        internal static CloudSyncAttributes ToCloudSyncAttributes(this ItemFieldDictionary itemFields)
        {
            return new CloudSyncAttributes(itemFields.GetName(), itemFields.GetLastModifiedUtc(), itemFields.GetETag());
        }

        internal static ItemFieldDictionary ToItemFieldDictionary(this CloudSyncAttributes attributes)
        {
            return new ItemFieldDictionary
                {
                    new ItemField(CloudSyncItemFields.Name, typeof (String), attributes.Name),
                    new ItemField(CloudSyncItemFields.Timestamp, typeof (UInt64), (UInt64)attributes.LastModifiedUtc.ToUniversalTime().Ticks)
                };
        }
        
        internal static String GetName(this ItemFieldDictionary itemFields)
        {
            return (String)itemFields.GetValue(CloudSyncItemFields.Name).Value;
        }

        internal static DateTime GetLastModifiedUtc(this ItemFieldDictionary itemFields)
        {
            return new DateTime((Int64)itemFields.GetValue(CloudSyncItemFields.Timestamp).Value.As<UInt64>(), DateTimeKind.Utc);
        }
        
        internal static String GetETag(this ItemFieldDictionary itemFields)
        {
            return String.Empty; 
        }

        private static ItemField GetValue(this ItemFieldDictionary itemFields, UInt32 key)
        {
            return (itemFields as IDictionary<UInt32, ItemField>)[key];
        }
    }
}